<?php

namespace App\Http\Controllers\API;

use App\Http\Requests\API\CreateHouseAPIRequest;
use App\Http\Requests\API\UpdateHouseAPIRequest;
use App\Models\House;
use App\Repositories\HouseRepository;
use Illuminate\Http\Request;
use App\Http\Controllers\AppBaseController;
use InfyOm\Generator\Criteria\LimitOffsetCriteria;
use Prettus\Repository\Criteria\RequestCriteria;
use Response;

/**
 * Class HouseController
 * @package App\Http\Controllers\API
 */

class HouseAPIController extends AppBaseController
{
    /** @var  HouseRepository */
    private $houseRepository;

    public function __construct(HouseRepository $houseRepo)
    {
        $this->houseRepository = $houseRepo;
    }

    /**
     * @param Request $request
     * @return Response
     *
     * @SWG\Get(
     *      path="/houses",
     *      summary="Get a listing of the Houses.",
     *      tags={"House"},
     *      description="Get all Houses",
     *      produces={"application/json"},
     *      @SWG\Response(
     *          response=200,
     *          description="successful operation",
     *          @SWG\Schema(
     *              type="object",
     *              @SWG\Property(
     *                  property="success",
     *                  type="boolean"
     *              ),
     *              @SWG\Property(
     *                  property="data",
     *                  type="array",
     *                  @SWG\Items(ref="#/definitions/House")
     *              ),
     *              @SWG\Property(
     *                  property="message",
     *                  type="string"
     *              )
     *          )
     *      )
     * )
     */
    public function index(Request $request)
    {
        $this->houseRepository->pushCriteria(new RequestCriteria($request));
        $this->houseRepository->pushCriteria(new LimitOffsetCriteria($request));
        $houses = $this->houseRepository->orderBy('id', 'desc')->simplePaginate($request->input('limit', 20));
        if ($houses->items())
        {
            foreach ($houses as $key => $val)
            {
                $houses[$key]->cover = fileUrl($val->cover);
            }
        }

        return $this->sendResponse($houses->toArray(), trans('flash.api.retrieve_success', ['model' => 'Houses']));
    }

    /**
     * @param CreateHouseAPIRequest $request
     * @return Response
     *
     * @SWG\Post(
     *      path="/houses",
     *      summary="Store a newly created House in storage",
     *      tags={"House"},
     *      description="Store House",
     *      produces={"application/json"},
     *      @SWG\Parameter(
     *          name="body",
     *          in="body",
     *          description="House that should be stored",
     *          required=false,
     *          @SWG\Schema(ref="#/definitions/House")
     *      ),
     *      @SWG\Response(
     *          response=200,
     *          description="successful operation",
     *          @SWG\Schema(
     *              type="object",
     *              @SWG\Property(
     *                  property="success",
     *                  type="boolean"
     *              ),
     *              @SWG\Property(
     *                  property="data",
     *                  ref="#/definitions/House"
     *              ),
     *              @SWG\Property(
     *                  property="message",
     *                  type="string"
     *              )
     *          )
     *      )
     * )
     */
    public function store(CreateHouseAPIRequest $request)
    {
        $input = $request->all();
        if (! fileUrlExists($input['cover']))
        {
            return $this->sendError('房源图片不存在');
        }

        $houses = $this->houseRepository->create($input);
        $houses->cover_path = $houses->cover;
        $houses->cover = fileUrl($houses->cover);

        return $this->sendResponse($houses->toArray(), trans('flash.api.save_success', ['model' => 'House']));
    }

    /**
     * @param int $id
     * @return Response
     *
     * @SWG\Get(
     *      path="/houses/{id}",
     *      summary="Display the specified House",
     *      tags={"House"},
     *      description="Get House",
     *      produces={"application/json"},
     *      @SWG\Parameter(
     *          name="id",
     *          description="id of House",
     *          type="integer",
     *          required=true,
     *          in="path"
     *      ),
     *      @SWG\Response(
     *          response=200,
     *          description="successful operation",
     *          @SWG\Schema(
     *              type="object",
     *              @SWG\Property(
     *                  property="success",
     *                  type="boolean"
     *              ),
     *              @SWG\Property(
     *                  property="data",
     *                  ref="#/definitions/House"
     *              ),
     *              @SWG\Property(
     *                  property="message",
     *                  type="string"
     *              )
     *          )
     *      )
     * )
     */
    public function show($id)
    {
        /** @var House $house */
        $house = $this->houseRepository->findWithoutFail($id);

        if (empty($house)) {
            return $this->sendError(trans('flash.api.not_found', ['model'=>'House']), 404);
        }

        return $this->sendResponse($house->toArray(), trans('flash.api.retrieve_success', ['model' => 'House']));
    }

    /**
     * @param int $id
     * @param UpdateHouseAPIRequest $request
     * @return Response
     *
     * @SWG\Put(
     *      path="/houses/{id}",
     *      summary="Update the specified House in storage",
     *      tags={"House"},
     *      description="Update House",
     *      produces={"application/json"},
     *      @SWG\Parameter(
     *          name="id",
     *          description="id of House",
     *          type="integer",
     *          required=true,
     *          in="path"
     *      ),
     *      @SWG\Parameter(
     *          name="body",
     *          in="body",
     *          description="House that should be updated",
     *          required=false,
     *          @SWG\Schema(ref="#/definitions/House")
     *      ),
     *      @SWG\Response(
     *          response=200,
     *          description="successful operation",
     *          @SWG\Schema(
     *              type="object",
     *              @SWG\Property(
     *                  property="success",
     *                  type="boolean"
     *              ),
     *              @SWG\Property(
     *                  property="data",
     *                  ref="#/definitions/House"
     *              ),
     *              @SWG\Property(
     *                  property="message",
     *                  type="string"
     *              )
     *          )
     *      )
     * )
     */
    public function update($id, UpdateHouseAPIRequest $request)
    {
        $input = $request->all();

        /** @var House $house */
        $house = $this->houseRepository->findWithoutFail($id);

        if (empty($house)) {
            return $this->sendError(trans('flash.api.not_found', ['model'=>'House']), 404);
        }

        if (! fileUrlExists($input['cover']))
        {
            return $this->sendError('房源图片不存在');
        }

        $house = $this->houseRepository->update($input, $id);
        $house->cover_path = $house->cover;
        $house->cover = fileUrl($house->cover);

        return $this->sendResponse($house->toArray(), trans('flash.api.update_success', ['model' => 'House']));
    }

    /**
     * @param int $id
     * @return Response
     *
     * @SWG\Delete(
     *      path="/houses/{id}",
     *      summary="Remove the specified House from storage",
     *      tags={"House"},
     *      description="Delete House",
     *      produces={"application/json"},
     *      @SWG\Parameter(
     *          name="id",
     *          description="id of House",
     *          type="integer",
     *          required=true,
     *          in="path"
     *      ),
     *      @SWG\Response(
     *          response=200,
     *          description="successful operation",
     *          @SWG\Schema(
     *              type="object",
     *              @SWG\Property(
     *                  property="success",
     *                  type="boolean"
     *              ),
     *              @SWG\Property(
     *                  property="data",
     *                  type="string"
     *              ),
     *              @SWG\Property(
     *                  property="message",
     *                  type="string"
     *              )
     *          )
     *      )
     * )
     */
    public function destroy($id)
    {
        /** @var House $house */
        $house = $this->houseRepository->findWithoutFail($id);

        if (empty($house)) {
            return $this->sendError('House not found', 404);
        }

        $house->delete();

        return $this->sendResponse($id, trans('flash.api.delete_success', ['model' => 'House']));
    }
}
